package com.gsn.poker.play;

import java.io.File;

import com.badlogic.gdx.files.FileHandle;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.scenes.scene2d.Group;
import com.badlogic.gdx.scenes.scene2d.ui.Image;
import com.badlogic.gdx.scenes.scene2d.ui.Label;
import com.gsn.desktop.engine.ActorUtility;
import com.gsn.engine.myplay.GsnGame;
import com.gsn.engine.template.GsnLabel;
import com.gsn.engine.utility.IUtility.IPictureDownloadListener;
import com.gsn.engine.utility.UtilityGettor;
import com.gsn.poker.asset.PokerTexture;

public class AvatarUser extends Group implements IPictureDownloadListener {
	GsnLabel nameLabel;
	GsnLabel goldLabel;
	int type;
	long gold;
	String name;
	Image avatar;
	Image contentAvatar;
	private File avatarFile;
	public String avatarUrl;

	public AvatarUser(int type) {
		transform = GsnGame.transform;
		avatar = new Image(PokerTexture.inst().avatarBG);
		this.type = type;
		addActor(avatar);
		this.width = avatar.width;
		this.height = avatar.height;

		// * 2;
		// this.height = PokerTexture.inst().Time.get(0).getRegionHeight() +
		// PokerTexture.inst().fontMedium.getLineHeight() * (1.0f);
		ActorUtility.setRatio(avatar, 0, 0, 0, 0);
	}

	public void setName(String name) {
		this.name = name;
		if (nameLabel != null)
			nameLabel.remove();
		nameLabel = new GsnLabel(" " + ActorUtility.getShortName(name) + " ", PokerTexture.inst().fontMedium, new Color(1f, 1f, 1f, 1f));
		// ActorUtility.setRatio(nameLabel, 0.4f, 0.6f, width / 2, height);
		// if (type == 0)
		ActorUtility.setRatio(nameLabel, 0.5f, 0.0f, width / 2, height);
		// else
		// ActorUtility.setRatio(nameLabel, 1.0f, 0.0f, width , height);
		addActor(nameLabel);
	}

	public void setGold(long gold) {
		this.gold = gold;
		if (goldLabel != null)
			goldLabel.remove();
		goldLabel = new GsnLabel(" $" + ActorUtility.formatNumber(gold) + " ", PokerTexture.inst().fontSmall, new Color(1f, 1f, 0f, 1f));
		if (type == 0 || type == -1)
			ActorUtility.setRatio(goldLabel, 0.f, 0.8f, -0.1f * width, 0);
		else
			ActorUtility.setRatio(goldLabel, 1f, 0.8f, 1.1f * width, 0);
		addActor(goldLabel);
	}

	public void setAvatar(String avatar) {
		avatarUrl = avatar;
		UtilityGettor.inst().downloadPictureAsync("me", avatar, 64, 64, name + ".png", this);
	}

	@Override
	public void onError(Exception e) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onFinishLoading(String id, File outFile) {
		avatarFile = outFile;
	}

	@Override
	public void act(float delta) {
		// TODO Auto-generated method stub
		super.act(delta);
		if (avatarFile != null) {
			try {
				Texture texture = new Texture(new FileHandle(avatarFile));
				TextureRegion region = new TextureRegion(texture);
				avatar.setRegion(region);
				avatarFile = null;
			} catch (Exception e) {
				avatarFile.delete();
				e.printStackTrace();
			} finally {
				avatarFile = null;
			}
		}

	}

	public long getMoney() {
		return gold;
	}
}
